package com.lovo.demo;

import javax.swing.JOptionPane;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.lovo.util.ArrayUtil;

public class Employee {
	public static String[][] strs = new String[7][];
	public static String[] receiptNum = {"取货码"}; // 取货码
	public static String[] receiptMan = {"收货人"}; // 收货人
	public static String[] receiptManPhone = {"收货人电话"}; //收货人电话
	public static String[] putTime = {"入库时间"}; // 入库时间
	public static String[] pickTime = {"取货时间"}; // 取货时间
	public static String[] pickMan = {"取货人"}; // 取货人
	public static String[] operator = {"经办人"}; // 经办人
	
	public static String[][] fnc(){
		strs[0] = receiptNum;
		strs[1] = receiptMan;
		strs[2] = receiptManPhone;
		strs[3] = putTime;
		strs[4] = pickTime;
		strs[5] = pickMan;
		strs[6] = operator;
		return strs;
	}
	
	public static void employeeMenu() {
		while(true){
			String input = JOptionPane.showInputDialog(null, "欢迎使用员工管理\n\n"
					+ "1.入库\n"
					+ "2.客户取货\n"
					+ "3.查看未收货订单\n"
					+ "4.按取货码查询订单\n"
					+ "5.按姓名查询订单\n"
					+ "6.修改密码\n"
					+ "7.安全退出");
			
			if(input == null)
				input = "";
			
			switch(input){
				case "1": inBase();
					break;
				case "2": get();
					break;
				case "3": select();
					break;
				case "4": selectByReceiptNum();
					break;
				case "5": selectByReceiptMan();
					break;
				case "6": Admin.updatePwd();
					break;
				case "7": ComSystemDemo.process();
					break;
				default: JOptionPane.showMessageDialog(null, "输入错误");
					break;
			}
		}
	}

	public static void selectByReceiptMan() {
		String name = JOptionPane.showInputDialog(null, "请输入收货人姓名");
		strs = fnc();
		String str = "";
		
		for (int i = 0; i < receiptMan.length; i++) {
			if(receiptMan[i].equals(name)){
				str = str + show(i) + "\n";
			}
		}
		str = show(str);
		JOptionPane.showMessageDialog(null, str);
	}
	
	public static void selectByReceiptNum() {
		int index = haveReceiptNum();
		if(index != -1){
			String str = show(0) + "\n" + show(index);
			JOptionPane.showMessageDialog(null, str);
		}else{
			JOptionPane.showMessageDialog(null, "取货码不存在");
		}
	}
	
	public static void select() {
		strs = fnc();
		String str = "";
		
		for (int i = 0; i < pickTime.length; i++) {
			if(pickTime[i].equals("no")){
				str = str + show(i) + "\n";
			}
		}
		str = show(str);
		JOptionPane.showMessageDialog(null, str);
	}

	public static void get() {		
		int index = haveReceiptNum();
		if(index != -1){
			String str = show(0)+"\n"+show(index);
			// 取货人信息不为no是表示货物已被取走
			if(pickMan[index].equals("no")){
				str = str + "\n\n确定取走吗";
				int isSure = JOptionPane.showConfirmDialog(null, str);
				
				while(isSure == 0){
					String name = JOptionPane.showInputDialog(null, "取货人签字");
					if(name == null || "".equals(name)){
						JOptionPane.showMessageDialog(null, "取货人姓名不能为空");
						continue;
					}
					
					pickMan[index] = name;
					isSure = -1;
					operator[index] = ComSystemDemo.getUsers()[1][ComSystemDemo.getIndex()];
					pickTime[index] = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
					JOptionPane.showMessageDialog(null, "取货成功");
				}
			}else{
				str = str +"\n\n该货物已被取走";
				JOptionPane.showMessageDialog(null, str);
			}
		}else{
			JOptionPane.showMessageDialog(null, "取货码不存在");
		}
	}

	public static void inBase() {
		String num = JOptionPane.showInputDialog(null, "请输入取货码");
		String user = JOptionPane.showInputDialog(null, "请输入收货人");
		String userNum = JOptionPane.showInputDialog(null, "请输入收货人电话");
		
		if((num == null || "".equals(num)) || (user == null || "".equals(user)) || (userNum == null || "".equals(userNum))){
			JOptionPane.showMessageDialog(null, "输入错误！！ 信息不能为空");
		}else{
			String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			receiptNum = ArrayUtil.add(receiptNum, num);
			receiptMan = ArrayUtil.add(receiptMan, user);
			receiptManPhone = ArrayUtil.add(receiptManPhone, userNum);
			putTime = ArrayUtil.add(putTime, today);
			pickTime = ArrayUtil.add(pickTime, "no");
			pickMan = ArrayUtil.add(pickMan, "no");
			operator = ArrayUtil.add(operator, "no");
			JOptionPane.showMessageDialog(null, "入库成功");
		}
	}
	
	//字符串拼接
	public static String show(int index){
		String str = "";
		for (int j = 0; j < strs.length; j++) {
			str = str +"    "+ strs[j][index];
		}
		return str;
	}
	
	public static String show(String str){
		if("".equals(str) || str == null){
			str = str + "无";
		}else{
			str = show(0) + "\n" + str;
		}
		
		return str;
	}
	
	//查询取货码是否存在 若存在则返回下标，不存在返回-1
	public static int haveReceiptNum(){
		String num = JOptionPane.showInputDialog(null, "请输入取货码");	
		strs = fnc();
		
		int index = -1;
		for (int i = 0; i < receiptNum.length; i++) {
			if(receiptNum[i].equals(num)){
				index = i;
				break;
			}
		}
		
		return index;
	}
}
